package com.seven.springboot.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateUtils {
	
	private static Logger LOGGER = LoggerFactory.getLogger(DateUtils.class);

	private final static ThreadLocal<SimpleDateFormat> TL_yyyyMMddHHmmss = new ThreadLocal<SimpleDateFormat>() {
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
		}
	};

	private final static ThreadLocal<SimpleDateFormat> TL_yyyyMMdd = new ThreadLocal<SimpleDateFormat>() {
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
		}
	};

	private final static ThreadLocal<SimpleDateFormat> TL_yyyyMd = new ThreadLocal<SimpleDateFormat>() {
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy-M-d", Locale.ENGLISH);
		}
	};

	private final static ThreadLocal<SimpleDateFormat> TL_HHmmss = new ThreadLocal<SimpleDateFormat>() {
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
		}
	};
	
	private final static ThreadLocal<SimpleDateFormat> TL_HHmm = new ThreadLocal<SimpleDateFormat>() {
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("HH:mm", Locale.ENGLISH);
		}
	};
	
	private final static ThreadLocal<SimpleDateFormat> TL_yyyyMMddHHmmssS = new ThreadLocal<SimpleDateFormat>() {
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss S", Locale.ENGLISH);
		}
	};

	/**
	 * 
	 * {获取当前时间并转换成如2018-06-01 15:06:37字符串}
	 * 
	 * @return
	 */
	public final static String date2String() {

		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		Calendar calendar = Calendar.getInstance();
		dateFormat.setLenient(false);
		Date timeDate = calendar.getTime();
		return dateFormat.format(timeDate);
	}

	public static int getCurrentMonth() {
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH) + 1;
		return month;
	}

	public static String getCurrentMonthStr() {
		int month = getCurrentMonth();
		if (month < 10) {
			return "0" + month;
		}
		return String.valueOf(month);
	}

	/**
	 * yyyy-MM-dd HH:mm:ss 变yyyy-MM-dd
	 *
	 * @param dateTime
	 * @return
	 * @throws ParseException
	 */
	public static String format2ShortDateString(String dateTime) throws ParseException {
		if (StringUtils.isEmpty(dateTime)) {
			return null;
		}
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		Date date = dateFormat.parse(dateTime);
		return date2String(date);
	}

	/**
	 * yyyy-MM-dd 转MM-dd
	 *
	 * @author daidalei
	 * @date 2016-12-19 下午6:56:17
	 */
	public final static String format2MonthAndDay(String dateString) {

		String result = null;
		try {
			SimpleDateFormat parseFormat = new SimpleDateFormat("MM-dd", Locale.ENGLISH);
			result = parseFormat.format(stringForDate(dateString));
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		return result;
	}
	/**
	 * 返回年月中文格式
	 * @author jiliangshan
	 * @date 2018年5月18日 下午3:06:32
	 */
	public final static String format2MonthAndDayCn(String dateString) {

		String result = null;
		try {
			SimpleDateFormat parseFormat = new SimpleDateFormat("MM-dd", Locale.ENGLISH);
			result = parseFormat.format(stringForDate(dateString));
			if(result == null) return null;
			String[] split = result.split("-");
			if(split != null && split.length == 2) {
				String monthStr = split[0];
				String dayStr = split[1];
				if(monthStr.startsWith("0")){
					monthStr = String.valueOf(monthStr.charAt(1));
				}
				if(dayStr.startsWith("0")){
					dayStr = String.valueOf(dayStr.charAt(1));
				}
				return monthStr + "月" + dayStr + "日";
			}
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		return result;
	}

	public static void main(String[] args) throws Exception {
		/*long now = System.currentTimeMillis();
		System.out.println(getHHMMSS(now));
		String fhTime = "08:00";
		System.out.println(getHHMMSS(fhTime));*/
		String timeMs = null;
        try {
            timeMs = getTimeMs("201807310910");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(timeMs);
	}

	/**
	 * 获取当前时间前一个月，并转换为转MM-dd格式
	 *
	 * @return
	 */
	public final static String getLastMonthDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, -1);
		return sdf.format(cal.getTime());
	}
	public final static String getLastMonthDateStr() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, -1);
		return sdf.format(cal.getTime());
	}

	/**
	 *
	 * {获取当前时间并转换成如2011-03-16字符串}
	 *
	 * @return
	 */
	public final static String shortDate2String() {

		Calendar calendar = Calendar.getInstance();
		DateFormat dateFormat = TL_yyyyMMdd.get();
		dateFormat.setLenient(false);
		Date timeDate = calendar.getTime();
		return dateFormat.format(timeDate);
	}

	/**
	 *
	 * {获取当前时间并转换成如HH:mm:ss字符串}
	 *
	 * @return
	 */
	public final static String shortTime2String() {
		Calendar calendar = Calendar.getInstance();
		DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
		dateFormat.setLenient(false);
		Date timeDate = calendar.getTime();
		return dateFormat.format(timeDate);
	}

	/**
	 *
	 * {将String类型的yyyy-MM-dd HH:mm:ss格式的时间转换成如HH:mm:ss字符串}
	 *
	 * @return
	 */
	public final static String stringDateTime2StringTime(Date dateTime){
		DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH);
		dateFormat.setLenient(false);
		return dateFormat.format(dateTime);
	}

	// 计算两个时间差，返回秒数, 参数格式："2004-03-26 13:31:40"
	public static long difftimes(String beforeTime, String afterTime) throws Exception {
		DateFormat df = TL_yyyyMMddHHmmss.get();
		Date d1 = df.parse(afterTime);// "2004-03-26 13:31:40"
		Date d2 = df.parse(beforeTime);// "2004-01-02 11:30:24"
		long diff = d1.getTime() - d2.getTime();
		long sends = diff / 1000;
		return sends;
	}

	// 计算两个时间差，返回毫秒数, 参数格式："2004-03-26 13:31:40"
	public static long diffMillsecond(String beforeTime, String afterTime) throws Exception {
		DateFormat df = TL_yyyyMMddHHmmss.get();
		Date d1 = df.parse(afterTime);// "2004-03-26 13:31:40"
		Date d2 = df.parse(beforeTime);// "2004-01-02 11:30:24"
		long diff = d1.getTime() - d2.getTime();
		return diff;
	}

	/**
	 *
	 * {获取当前时间并转换成如20110316121212586字符串}
	 *
	 * @return
	 */
	public final static String date2NoLineString() {
		Calendar calendar = Calendar.getInstance();
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.ENGLISH);
		dateFormat.setLenient(false);
		Date timeDate = calendar.getTime();
		return dateFormat.format(timeDate);
	}

	/**
	 *
	 * {将java.sql.Date转换成如2011-03-16字符串}
	 *
	 * @param date
	 * @return
	 */
	public final static String date2String(java.sql.Date date) {
		DateFormat dateFormat = TL_yyyyMMdd.get();
		dateFormat.setLenient(false);
		return dateFormat.format(date);
	}

	/**
	 *
	 * {将java.util.Date转换成如2011-03-16字符串}
	 *
	 * @param date
	 * @return
	 */
	public final static String date2String(Date date) {
		DateFormat dateFormat = TL_yyyyMMdd.get();
		dateFormat.setLenient(false);
		return dateFormat.format(date);
	}

	/**
	 *
	 * {将java.sql.Date转换成如2011-03-16字符串}
	 *
	 * @param date
	 * @return
	 */
	public final static String dateChangeString(Date date) {
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		dateFormat.setLenient(false);
		return dateFormat.format(date);
	}

	/**
	 *
	 * yyyyMMdd
	 *
	 * @author tian 2013-5-14
	 * @param date
	 * @return
	 */
	public final static String dateToString(Date date) {
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
		dateFormat.setLenient(false);
		return dateFormat.format(date);
	}

	/**
	 *
	 * yyyyMMdd
	 *
	 * @param date
	 * @return
	 */
	public final static Date stringToUtilDate(String date) {
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
		dateFormat.setLenient(false);
		try {
			return dateFormat.parse(date);
		} catch (ParseException e) {

		}
		return null;
	}

	/**
	 * 格式化如20161-1-1格式的日期
	 *
	 * @param date
	 * @return
	 */
	public final static String formatDate(String date) {

		if(date.indexOf("-") < 0) {
			throw new IllegalArgumentException("date format is not legal, date: " + date);
		}

		String[] dt = date.split("-");

		String d1 = dt[1];
		if (d1.length() == 1) {
			d1 = "0" + d1;
		}
		String d2 = dt[2];
		if (d2.length() == 1) {
			d2 = "0" + d2;
		}
		String dd = dt[0] + "-" + d1 + "-" + d2;
		return dd;
	}

	/**
	 *
	 * {获取当前时间并转换成如20110316字符串}
	 *
	 * @return
	 */
	public final static String dateToString() {
		Calendar calendar = Calendar.getInstance();
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
		dateFormat.setLenient(false);
		Date timeDate = calendar.getTime();
		return dateFormat.format(timeDate);
	}

	public final static String dateForString(Date date) {
		DateFormat dateFormat = new SimpleDateFormat("yyyyMM", Locale.ENGLISH);
		dateFormat.setLenient(false);
		return dateFormat.format(date);
	}

	/**
	 *
	 * {将Timestamp转换成如2011-03-16 12:12:12字符串}
	 *
	 * @param timestamp
	 * @return
	 */
	public final static String timestamp2String(java.sql.Timestamp timestamp) {
		if (timestamp == null) {
			return null;
		}
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		dateFormat.setLenient(false);
		return dateFormat.format(timestamp);
	}

	public static Date string2datetime(String datetime) throws ParseException {
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		return dateFormat.parse(datetime);
	}

	public static Date string2datetime_yyyyMMdd(String datetime) throws ParseException {
		DateFormat dateFormat = TL_yyyyMMdd.get();
		return dateFormat.parse(datetime);
	}

	/**
	 * 将字符串类型的HH:mm类型时间转为java.util.Date类型的HH:mm时间
	 * @param datetime
	 * @return
	 * @throws ParseException
	 */
	public static Date string2DateTime_HHmmss(String datetime) throws ParseException {
		DateFormat dateFormat = TL_HHmmss.get();
		return dateFormat.parse(datetime);
	}

	public static String datetime2string(Date date) {
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		return dateFormat.format(date);
	}

	public static String datetime2string(java.sql.Date date) {

		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		return dateFormat.format(date);
	}

	public final static String timestamp2Short(java.sql.Timestamp timestamp) {
		if (timestamp == null) {
			return null;
		}
		DateFormat dateFormat = TL_yyyyMMdd.get();
		dateFormat.setLenient(false);
		return dateFormat.format(timestamp);
	}

	public final static String timestamp2ZHShort(java.sql.Timestamp timestamp) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日", Locale.ENGLISH);
		dateFormat.setLenient(false);
		return dateFormat.format(timestamp);
	}

	/**
	 *
	 * {将字符串类型的日期转换成一个Date(java.sql.Date}
	 *
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public final static java.sql.Date string2Date(String dateString) throws ParseException {

		DateFormat dateFormat = TL_yyyyMMdd.get();
		// dateFormat.setLenient(false); //严格解析
		Date timeDate = dateFormat.parse(dateString);
		java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());

		return dateTime;

	}

	/**
	 *
	 * {将字符串类型的日期转换成一个Date(java.util.Date}
	 *
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public final static Date stringForDate(String dateString) throws ParseException {

		DateFormat dateFormat = TL_yyyyMMdd.get();
		dateFormat.setLenient(false); // 严格解析
		Date timeDate = dateFormat.parse(dateString);
		return timeDate;

	}

	/**
	 *
	 * {将字符串类型的日期转换成一个Date(java.util.Date}
	 *
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public final static Date stringFormatDate(String dateString) throws ParseException {

		DateFormat dateFormat = TL_yyyyMMdd.get();
		// dateFormat.setLenient(false); //严格解析
		Date timeDate = dateFormat.parse(dateString);
		return timeDate;

	}

	/**
	 *
	 * {将字符串类型的日期转换成一个String} 格式：yyyy-MM-dd HH:mm:ss
	 *
	 * @return
	 * @throws ParseException
	 */
	public final static String stringFormatDateYYYY(String dateString, boolean isbegin) throws ParseException {

		Date datetime = null;
		//trim
		dateString = dateString.trim();
		DateFormat parseFormat = TL_yyyyMMddHHmmss.get();
		if (isbegin) {
			datetime = parseFormat.parse(dateString + " 00:00:00");
		} else {
			datetime = parseFormat.parse(dateString + " 23:59:59");
		}
		return parseFormat.format(datetime);
	}

	public final static String formatDateStr(String dateString) throws ParseException {

		DateFormat parseFormat = TL_yyyyMMdd.get();
		Date timeDate = parseFormat.parse(dateString);
		return parseFormat.format(timeDate);
	}

	/**
	 * yyyy/MM/dd转换为yyyy-MM-dd
	 *
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public final static String formatDateStr2(String dateString) throws ParseException {

		DateFormat parseFormat = TL_yyyyMMdd.get();
		DateFormat parseFormat2 = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
		Date timeDate = parseFormat2.parse(dateString);
		return parseFormat.format(timeDate);
	}

	/**
	 * yyyy-MM-dd转换为yyyy/MM/dd
	 *
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public final static String formatDateStr0(String dateString) throws ParseException {

		DateFormat parseFormat = TL_yyyyMMdd.get();
		DateFormat parseFormat2 = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
		Date timeDate = parseFormat.parse(dateString);
		return parseFormat2.format(timeDate);
	}

	public final static String formatDateStr1(String dateString) throws ParseException {

		DateFormat parseFormat = TL_yyyyMMdd.get();
		DateFormat parseFormat2 = new SimpleDateFormat("MM/dd", Locale.ENGLISH);
		Date timeDate = parseFormat.parse(dateString);
		return parseFormat2.format(timeDate);
	}

	/**
	 * {将字符串类型的日期转换成一个String} 格式：YYYY-M-d
	 *
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public final static String formatDateYYYYMd(String dateString) throws ParseException {

		DateFormat parseFormat = new SimpleDateFormat("yyyy-M-d");
		Date timeDate = parseFormat.parse(dateString);
		return parseFormat.format(timeDate);
	}

	/**
	 *
	 * {将字符串类型的日期转换成一个String} 格式：YYYY-MM月dd日
	 *
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public final static String stringFormatDateCN(String dateString) throws ParseException {
		if (StringUtils.isEmpty(dateString)){
		    return null;
		}
		DateFormat parseFormat = TL_yyyyMMdd.get();
		DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
		Date timeDate = parseFormat.parse(dateString);

		return dateFormat.format(timeDate);
	}

	/**
	 *
	 * {将字符串类型的日期转换成一个Date(java.sql.Date}
	 *
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public final static java.sql.Date stringToDate(String dateString) throws ParseException {

		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
		// dateFormat.setLenient(false); //严格解析
		Date timeDate = dateFormat.parse(dateString);
		java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());
		return dateTime;

	}

	/**
	 *
	 * {将字符串类型的日期转换成一个timestamp(java.sql.Timestamp)}
	 *
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public final static java.sql.Timestamp string2Timestamp(String dateString) throws ParseException {

		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		dateFormat.setLenient(false);
		Date timeDate = dateFormat.parse(dateString);
		java.sql.Timestamp dateTimestamp = new java.sql.Timestamp(timeDate.getTime());
		return dateTimestamp;
	}

	/**
	 * 把当天时间往后推迟i天,返回yyyy-MM-dd类型的字符串
	 *
	 * @author tian
	 */
	public static String getFutureTime(int i) {

		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String dateString = formatter.format(date);
		return dateString;

	}

	public static String getFutureTimeMmdd(int i) {
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat formatter = new SimpleDateFormat("MM-dd");
		String dateString = formatter.format(date);
		return dateString;

	}

	/**
	 * 把当天时间往后推迟i天
	 *
	 * @author tian
	 */
	public static Date getFutureTimeDate(int i) {

		Date result = null;
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String dateString = formatter.format(date);
		try {
			result = formatter.parse(dateString);
		} catch (ParseException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return result;
	}

	/**
	 * 得到指定时间之后一周的时间
	 *
	 * @return
	 * @throws ParseException
	 */
	public static String getTimeOfOneWeekString(String dateString) throws ParseException {
		return getFutureTime(string2Date(dateString), 6);
	}

	/**
	 * 得到指定时间之后一周的时间
	 *
	 * @return
	 * @throws ParseException
	 */
	public static Date getTimeOfOneWeekDate(String dateString) throws ParseException {
		return string2Date(getTimeOfOneWeekString(dateString));
	}

	/**
	 * 得到明天
	 *
	 * @return
	 */
	public static String getTorrowString() {
		return getFutureTime(1);
	}

	/**
	 * 得到明天
	 *
	 * @return
	 */
	public static Date getTorrowDate() {
		try {
			return TL_yyyyMMdd.get().parse(getTorrowString());
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 得到今天的字符串
	 *
	 * @return
	 */
	public static String getTodayString() {
		return getFutureTime(0);
	}

	/**
	 * 得到今天的Date
	 *
	 * @return
	 */
	public static Date getTodayDate() {
		String futureTime = getFutureTime(0);

		try {
			return TL_yyyyMMdd.get().parse(futureTime);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 把指定时间往后推迟i天
	 *
	 * @author QinQichang
	 */
	public static String getFutureTime(Date date, int i) {

		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String dateString = formatter.format(date);
		return dateString;

	}

	/**
	 * 把指定时间往后推迟i天
	 *
	 * @author LiangChengjian
	 * @throws ParseException
	 */
	public static Date getFutureDateTime(String dateTime, int i) throws ParseException {

	    Date date = stringFormatToDateTime(dateTime);
	    Calendar calendar = new GregorianCalendar();
	    calendar.setTime(date);
	    calendar.add(Calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动
	    date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
	    return date;

	}

	/**
	 * 将指定日期往后推迟i天
	 * @throws ParseException
	 * @date 2017年12月14日
	 */
	public static String getFutureDate(String dateString, int i) throws ParseException {

		Date date = stringForDate(dateString);
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String futureDate = formatter.format(date);
		return futureDate;
	}



	/**
	 * 获取当前是星期几
	 *
	 * @author tian
	 */
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	public static String getMiddleWeekOfDate(Date dt) {
		String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	public static String getMiddleWeekOfDateStr(String dt) throws ParseException {
		Date date = stringFormatDate(dt);
		String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}


	/**
	 * 获取当前是星期几
	 *
	 * @author tian
	 */
	public static String getShortWeekOfDate(Date dt) {
		String[] weekDays = { "日", "一", "二", "三", "四", "五", "六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	/**
	 * 获取当前是星期几（数字型）
	 *
	 * @param dt
	 * @return
	 */
	public static String getNumWeekOfDate(Date dt) {
		String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	/**
	 * 获取当前是星期几（数字型）
	 *
	 * @param date
	 * @return
	 */
	public static int getNumWeekOfDate(String date) {
		try {
			int[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
			Calendar cal = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date dt = sdf.parse(date);
			cal.setTime(dt);
			int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
			if (w < 0)
				w = 0;
			return weekDays[w];
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 传入时间和当前时间比较是否超期 超时:true overNum 单位是秒
	 *
	 * @author tian
	 */
	public static boolean checkOverTime(Date date, Integer overNum) {
		if (date != null) {
			Long times = new Date().getTime() - date.getTime();
			return (times > (overNum * 1000)) ? true : false;
		}
		return false;
	}
	/**
	 * 传入时间和当前时间比较是否超期
	 * @author jiliangshan
	 * @throws Exception
	 * @date 2018年6月4日 下午4:00:40
	 */
	public static boolean checkOverTime(String dateTime) throws Exception {
		Date date = getDate(dateTime);
		if (date != null) {
			Long times = new Date().getTime() - date.getTime();
			return (times > 0) ? true : false;
		}
		return false;
	}

	/**
	 * 判断当前时间是否是零点
	 *
	 * @author jiliangshan
	 * @date 2017年7月7日 下午12:00:04
	 * @return boolean
	 */
	public static boolean checkOverZeroPoint() {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String d = sdf.format(new Date());
		if (d.equals("00:00:00")) {
			return true;
		}
		return false;
	}

	/**
	 * 根据生日Date获得用户年龄
	 *
	 * @author tian
	 */
	public static float getExactAgeByBirthday(Date birthday) {
		Date date = new Date();
		long day = (date.getTime() - birthday.getTime()) / (24 * 60 * 60 * 1000) + 1;
		BigDecimal decimal = new BigDecimal(day / 365f);
		return decimal.setScale(2, RoundingMode.HALF_UP).floatValue();
	}

	/**
	 * 根据生日Date获得用户年龄
	 *
	 * @author tian
	 * @throws ParseException
	 */
	public static float getExactAgeByBirthday(String birthday) throws ParseException {

		Date birth = stringFormatDate(birthday);
		return getExactAgeByBirthday(birth);
	}

	/**
	 * 根据生日Date获得用户年龄
	 *
	 * @author tian
	 */
	public static String getAgeByBirthday(Date birthday) {
		Date date = new Date();
		long day = (date.getTime() - birthday.getTime()) / (24 * 60 * 60 * 1000) + 1;
		return new java.text.DecimalFormat("#").format(day / 365f);
	}

	/**
	 * 根据生日string获得用户年龄
	 *
	 * @author tian
	 * @throws ParseException
	 */
	public static String getAgeByBirthday(String birthday) throws ParseException {
		Date date = new Date();
		long day = (date.getTime() - string2Date(birthday).getTime()) / (24 * 60 * 60 * 1000) + 1;
		return new java.text.DecimalFormat("#").format(day / 365f);
	}

	/**
	 * 2013-08-27 15:12:50-->1377587570000
	 *
	 * @param datetime
	 * @return
	 * @throws ParseException
	 */
	public static long StringDateToLong(String datetime) throws ParseException {
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		return dateFormat.parse(datetime).getTime();
	}

	/**
	 * 2013-08-27-->1377587570000
	 *
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static long StringShortDateToLong(String date) throws ParseException {
		DateFormat dateFormat = TL_yyyyMMdd.get();
		return dateFormat.parse(date).getTime();
	}

	/**
	 * date-->yyyy-MM-dd HH:mm:ss
	 *
	 * @param date
	 * @return
	 */
	public static String DateToString(Date date) {
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		return dateFormat.format(date);
	}

	/**
	 * 与输入时间进行加减
	 *
	 * @param date
	 *            加减参数
	 * @param shortDate
	 *            参照时间
	 * @return 返回加减后的时间
	 */
	public static String getShortDate4Para(int date, String shortDate) {
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(TL_yyyyMMdd.get().parse(shortDate));
		} catch (Exception e) {
			throw new RuntimeException("日期转换出错!");
		}

		calendar.add(Calendar.DATE, date);

		return DateUtils.date2String(calendar.getTime());
	}

	/*
	 * 比较两个时间,yyyy-MM-dd HH:mm:ss
	 */
	public static int compareDate(String date1, String date2) throws ParseException {
		if (StringUtils.isEmpty(date1) && StringUtils.isEmpty(date2)) {
			return 0;
		}
		if (StringUtils.isEmpty(date1)) {
			return 1;
		}
		if (StringUtils.isEmpty(date2)) {
			return -1;
		}
		DateFormat df = TL_yyyyMMddHHmmss.get();
		Date dt1 = df.parse(date1);
		Date dt2 = df.parse(date2);
		if (dt1.getTime() > dt2.getTime()) {
			return 1;
		} else if (dt1.getTime() < dt2.getTime()) {
			return -1;
		} else {
			return 0;
		}
	}

	public static int compareDate_yyyyMMdd(String date1, String date2) throws ParseException {
		if (StringUtils.isEmpty(date1) && StringUtils.isEmpty(date2)) {
			return 0;
		}
		if (StringUtils.isEmpty(date1)) {
			return 1;
		}
		if (StringUtils.isEmpty(date2)) {
			return -1;
		}
		DateFormat df = TL_yyyyMMdd.get();
		Date dt1 = df.parse(date1);
		Date dt2 = df.parse(date2);
		if (dt1.getTime() > dt2.getTime()) {
			return 1;
		} else if (dt1.getTime() < dt2.getTime()) {
			return -1;
		} else {
			return 0;
		}
	}

	/*
	 * 比较两个时间,yyyy-MM-dd HH:mm:ss
	 */
	public static int compareDateTimeLong(String date1, String date2) throws ParseException {
		if (StringUtils.isEmpty(date1) && StringUtils.isEmpty(date2)) {
			return 0;
		}
		if (StringUtils.isEmpty(date1)) {
			return 1;
		}
		if (StringUtils.isEmpty(date2)) {
			return -1;
		}
		DateFormat df = TL_yyyyMMddHHmmss.get();
		Date dt1 = df.parse(date1);
		Date dt2 = df.parse(date2);
		if (dt1.getTime() > dt2.getTime()) {
			return 1;
		} else if (dt1.getTime() < dt2.getTime()) {
			return -1;
		} else {
			return 0;
		}
	}
	/*
	 * 比较两个时间,yyyy-MM-dd
	 */
	public static int compareDateTime(String date1, String date2) throws ParseException {
		if (StringUtils.isEmpty(date1) && StringUtils.isEmpty(date2)) {
			return 0;
		}
		if (StringUtils.isEmpty(date1)) {
			return 1;
		}
		if (StringUtils.isEmpty(date2)) {
			return -1;
		}
		DateFormat df = TL_yyyyMMdd.get();
		Date dt1 = df.parse(date1);
		Date dt2 = df.parse(date2);
		if (dt1.getTime() > dt2.getTime()) {
			return 1;
		} else if (dt1.getTime() < dt2.getTime()) {
			return -1;
		} else {
			return 0;
		}
	}

	/*
	 * HH:mm:ss
	 */
	public static int compareTime(String date1, String date2) throws ParseException {
		if (StringUtils.isEmpty(date1) && StringUtils.isEmpty(date2)) {
			return 0;
		}
		if (StringUtils.isEmpty(date1)) {
			return 1;
		}
		if (StringUtils.isEmpty(date2)) {
			return -1;
		}
		DateFormat df = new SimpleDateFormat("HH:mm:ss");
		Date dt1 = df.parse(date1);
		Date dt2 = df.parse(date2);
		if (dt1.getTime() > dt2.getTime()) {
			return 1;
		} else if (dt1.getTime() < dt2.getTime()) {
			return -1;
		} else {
			return 0;
		}
	}

	/**
	 * 当前时间是否在某个日期时间段之间（yyyy-MM-dd HH:mm:ss）
	 *
	 * @author daidalei
	 * @date 2016-6-27 上午11:49:54
	 */
	public static boolean currentTimeIsBetweenTime(String startTime, String endTime) throws ParseException {
		Date date = new Date();
		DateFormat df = TL_yyyyMMddHHmmss.get();
		String currentTime = df.format(date);
		int result1 = compareDate(currentTime, startTime);
		if (result1 == -1) {
			return false;
		}
		int result2 = compareDate(currentTime, endTime);
		if (result2 == 1) {
			return false;
		}
		return true;
	}

	/**
	 * 当前时间是否在某个时间段之间（HH:mm:ss）
	 *
	 * @author daidalei
	 * @date 2016-6-27 上午11:49:54
	 */
	public static boolean currentDateIsBetweenTime(String startTime, String endTime) throws ParseException {
		String currentTime = shortTime2String();
		int result1 = compareTime(currentTime, startTime);
		if (result1 == -1) {
			return false;
		}
		int result2 = compareTime(currentTime, endTime);
		if (result2 == 1) {
			return false;
		}
		return true;
	}

	/**
	 * 当前时间是否在某个日期段之间（yyyy-MM-dd）
	 *
	 * @author daidalei
	 * @date 2016-6-27 上午11:49:54
	 */
	public static boolean currentDateIsBetweenDate(String startDate, String endDate) throws ParseException {
		Date date = new Date();
		DateFormat df = TL_yyyyMMdd.get();
		String currentTime = df.format(date);
		int result1 = compareDateTime(currentTime, startDate);
		if (result1 == -1) {
			return false;
		}
		int result2 = compareDateTime(currentTime, endDate);
		if (result2 == 1) {
			return false;
		}
		return true;
	}

	/*
	 * 比较两个时间, yyyy-MM-dd
	 */
	public static int compareShortDate(String date1, String date2) throws ParseException {
		if (StringUtils.isEmpty(date1) && StringUtils.isEmpty(date2)) {
			return 0;
		}
		if (StringUtils.isEmpty(date1)) {
			return -1;
		}
		if (StringUtils.isEmpty(date2)) {
			return 1;
		}
		DateFormat df = TL_yyyyMMdd.get();
		Date dt1 = df.parse(date1);
		Date dt2 = df.parse(date2);
		if (dt1.getTime() > dt2.getTime()) {
			return 1;
		} else if (dt1.getTime() < dt2.getTime()) {
			return -1;
		} else {
			return 0;
		}
	}

	/*
	 * 比较两个时间, yyyy-MM-dd，倒敘
	 */
	public static int compareShortDateDesc(String date1, String date2) throws ParseException {
		if (StringUtils.isEmpty(date1) && StringUtils.isEmpty(date2)) {
			return 0;
		}
		if (StringUtils.isEmpty(date1)) {
			return -1;
		}
		if (StringUtils.isEmpty(date2)) {
			return 1;
		}
		DateFormat df = TL_yyyyMMdd.get();
		Date dt1 = df.parse(date1);
		Date dt2 = df.parse(date2);
		if (dt1.getTime() > dt2.getTime()) {
			return -1;
		} else if (dt1.getTime() < dt2.getTime()) {
			return 1;
		} else {
			return 0;
		}
	}

	/**
	 * 将微信消息中的CreateTime转换成标准格式的时间（yyyy-MM-dd HH:mm:ss）
	 *
	 * @param createTime
	 *            消息创建时间
	 * @return
	 */
	public static String formatTime(long createTime) {

		// 将微信传入的CreateTime转换成long类型，再乘以1000

		// long msgCreateTime = Long.parseLong(createTime) * 1000L;

		DateFormat format = TL_yyyyMMddHHmmss.get();
		return format.format(new Date(createTime * 1000L));
	}

	/**
	 * 比较两个小时
	 *
	 * @param hour
	 *            要比较的时间
	 * @return 当前时间大于hour返回true， 反则返回false
	 */
	public static boolean compareHour(String hour) {

		if (StringUtils.isEmpty(hour)) {
			return false;
		}

		int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		int currentMinute = Calendar.getInstance().get(Calendar.MINUTE);

		int current = currentHour * 60 + currentMinute;

		String[] hours = hour.split(":");

		int dest = Integer.valueOf(hours[0]) * 60 + Integer.valueOf(hours[1]);

		return current - dest > 0;
	}

	/**
	 * 把从开始时间之后的指定连续天数根据指定拼接字符串组装成一个string 集合 日期格式:yyyy-MM-dd
	 *
	 * @param begin
	 *            从那天开始
	 * @param days
	 *            连续天数
	 * @return key-日期,value-星期几
	 */
	public static TreeMap<String, String> getContinuouskDayList(Date begin, int days) {
		TreeMap<String, String> map = new TreeMap<String, String>();
		for (int i = 0; i < days; i++) {
			Calendar calendar = new GregorianCalendar();
			DateFormat dateFormat = TL_yyyyMMdd.get();
			calendar.setTime(begin);
			calendar.add(Calendar.DAY_OF_MONTH, i);
			Date time = calendar.getTime();
			String dayString = dateFormat.format(time);
			map.put(dayString, getWeekOfDate(time));
		}
		return map;
	}

	/**
	 * 把从开始时间之后的指定连续天数根据指定拼接字符串组装成一个string 集合 日期格式:yyyy-MM-dd
	 *
	 * @param begin
	 *            从那天开始
	 * @param days
	 *            连续天数
	 * @return key-日期,value-星期几
	 */
	public static TreeMap<String, String> getContinuouskDayListWithShortWeekDate(Date begin, int days) {
		TreeMap<String, String> map = new TreeMap<String, String>();
		for (int i = 0; i < days; i++) {
			Calendar calendar = new GregorianCalendar();
			DateFormat dateFormat = TL_yyyyMMdd.get();
			calendar.setTime(begin);
			calendar.add(Calendar.DAY_OF_MONTH, i);
			Date time = calendar.getTime();
			String dayString = dateFormat.format(time);
			map.put(dayString, getShortWeekOfDate(time));
		}
		return map;
	}

	/**
	 * 把从开始时间之后的指定连续天数根据指定拼接字符串组装成一个string 集合 日期格式:yyyy-MM-dd
	 *
	 * @param begin
	 *            从那天开始
	 * @param days
	 *            连续天数
	 * @return key-日期,value- 1/2/3/4/5/6/7
	 */
	public static TreeMap<String, String> getContinuouskDayListWithNumWeekDate(Date begin, int days) {
		TreeMap<String, String> map = new TreeMap<String, String>();
		for (int i = 0; i < days; i++) {
			Calendar calendar = new GregorianCalendar();
			DateFormat dateFormat = TL_yyyyMMdd.get();
			calendar.setTime(begin);
			calendar.add(Calendar.DAY_OF_MONTH, i);
			Date time = calendar.getTime();
			String dayString = dateFormat.format(time);
			map.put(dayString, getNumWeekOfDate(time));
		}
		return map;
	}

	/**
	 * 获取指定日期的当月开始时间，
	 *
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss 格式
	 */
	public static String getMonthBegin(Date date) {
		DateFormat df = TL_yyyyMMdd.get();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		String result = df.format(calendar.getTime());
		return result + " 00:00:00";

	}

	/**
	 * 获取指定日期的当周开始时间， 中国式周，周一是第一天
	 *
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss 格式
	 */
	public static String getWeekBegin(Date date) {
		DateFormat df = TL_yyyyMMdd.get();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMinimum(Calendar.DAY_OF_WEEK));
		calendar.add(Calendar.DAY_OF_WEEK, 1);
		String result = df.format(calendar.getTime());
		return result + " 00:00:00";
	}

	/**
	 * 获取指定日期的当周结束时间 中国式周，周一是第一天
	 *
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss 格式
	 */
	public static String getWeekEnd(Date date) {
		DateFormat df = TL_yyyyMMdd.get();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
		calendar.add(Calendar.DAY_OF_WEEK, 1);
		String result = df.format(calendar.getTime());
		return result + " 23:59:59";
	}
	/**
	 * 获取当前时间所在周的周一
	 *
	 * @return yyyy-MM-dd HH:mm:ss 格式
	 */
	public static String getWeekStartDate(){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
        Calendar cal=Calendar.getInstance(Locale.CHINA);
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String result = df.format(cal.getTime());
		return result + " 00:00:00";
	}
	/**
	 * 获取指定日期的当月结束时间
	 *
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss 格式
	 */
	public static String getMonthEnd(Date date) {

		DateFormat df = TL_yyyyMMdd.get();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

		String result = df.format(calendar.getTime());
		return result + " 23:59:59";
	}

	/**
	 * 获取指定日期的当月开始时间，
	 *
	 * @param dateTime
	 *            yyyy-MM-dd HH:mm:ss 格式
	 * @return
	 * @throws ParseException
	 */
	public static String getMonthBegin(String dateTime) throws ParseException {
		DateFormat df = TL_yyyyMMddHHmmss.get();
		Date date = df.parse(dateTime);
		return getMonthBegin(date);

	}

	/**
	 * 获取指定日期的当月结束时间
	 *
	 * @param dateTime
	 *            yyyy-MM-dd HH:mm:ss 格式
	 * @return
	 * @throws ParseException
	 */
	public static String getMonthEnd(String dateTime) throws ParseException {

		DateFormat df = TL_yyyyMMddHHmmss.get();
		Date date = df.parse(dateTime);
		return getMonthEnd(date);
	}

	/**
	 * util date 转换成 sql date
	 *
	 * @param date
	 * @return
	 */
	public static java.sql.Date date2date(Date date) {

		if (date == null) {
			return null;
		}

		java.sql.Date sqlDate = new java.sql.Date(date.getTime());
		return sqlDate;
	}

	/**
	 * util date 转换成 sql date
	 *
	 * @param date
	 * @return
	 */
	public static Date date2date(java.sql.Date date) {

		if (date == null) {
			return null;
		}

		Date utilDate = new Date(date.getTime());
		return utilDate;
	}

	/**
	 * 判断字符串是否是符合某个格式的日期
	 * 
	 * @param startDate
	 * @param
	 */
	public static boolean isDate(String startDate, String pattern) {
		if (StringUtils.isEmpty(startDate)) {
			return false;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			sdf.setLenient(false);
			sdf.parse(startDate);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 判断字符串是否不符合某个格式的日期
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static boolean isNotDate(String date, String pattern) {
		return !isDate(date, pattern);
	}

    /**
     * 判断传入的String类型的time，例如："09:00"是否不满足HH:mm:ss格式,不满足会拼接":00" 并转换成java.util.Date类型
     * 注：仅限最短是"09:00"的格式判断及转换
     * @param time
     * @return
     */
    public static Date trans2StandardTime(String time) throws ParseException {
        boolean flag = DateUtils.isNotDate(time, "HH:mm:ss");
        if (flag) {
            time += ":00";
        }
        Date date = DateUtils.string2DateTime_HHmmss(time);
        return date;
    }

	public static Date getFutureTimeDate(Date begin, int i) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(begin);
		calendar.add(Calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动
		begin = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String dateString = formatter.format(begin);
		try {
			return formatter.parse(dateString);
		} catch (ParseException e) {
			return null;
		}
	}

	public static String getFutureTime(String startDay, int i) throws ParseException {
		
		Date date = stringFormatDate(startDay);
		return getFutureTime(date, i);
	}

	/**
	 * 获取n月的第一天，n=0表示当前月，n=1为下一个月，n=-1上一个月
	 * 
	 * @param n
	 * @return
	 */
	public static String getMonthFirstDay(int n) {
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		Calendar calendar = Calendar.getInstance();

		calendar.add(Calendar.MONTH, n);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
		calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
		calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
		calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));

		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 将数字转为中文星期数
	 * 
	 * @param dutyWeekDay
	 * @return
	 */
	public static String convertToChineseWeek(int dutyWeekDay) {
		switch (dutyWeekDay) {
		case 1:
			return "星期一";
		case 2:
			return "星期二";
		case 3:
			return "星期三";
		case 4:
			return "星期四";
		case 5:
			return "星期五";
		case 6:
			return "星期六";
		case 7:
			return "星期日";
		default:
			return "";
		}
	}

	/**
	 * 根据身份证计算年龄
	 * 
	 * @param cardNumber
	 * @return
	 * @throws ParseException 
	 */
	public static float getExactAgeByCardNumber(String cardNumber) throws ParseException {
		int age = 0;
		if (!ValidateUtils.isIdentityCard(cardNumber)) {
			return age;
		}
		String birthYear = cardNumber.substring(6, 10);
		String birthMonth = cardNumber.substring(10, 12);
		String birthDay = cardNumber.substring(12, 14);
		String birthDate = birthYear + "-" + birthMonth + "-" + birthDay;
		return getExactAgeByBirthday(birthDate);
	}

	/**
	 * 根据身份证计算年龄
	 * 
	 * @param cardNumber
	 * @return
	 * @throws ParseException 
	 * @throws NumberFormatException 
	 */
	public static int getAgeByCardNumber(String cardNumber) throws NumberFormatException, ParseException {
		int age = 0;
		if (!ValidateUtils.isIdentityCard(cardNumber)) {
			return age;
		}
		String birthYear = cardNumber.substring(6, 10);
		String birthMonth = cardNumber.substring(10, 12);
		String birthDay = cardNumber.substring(12, 14);
		String birthDate = birthYear + "-" + birthMonth + "-" + birthDay;
		age = Integer.parseInt(getAgeByBirthday(birthDate));
		return age;
	}

	/**
	 * 将身份证上的生日格式化成yyyy-MM-dd的格式
	 * 
	 * @param idno
	 * @return
	 */
	public static String formatIdentifiedCardForBirthday(String idno) {
		if (!ValidateUtils.isIdentityCard(idno)) {
			return null;
		}
		String birthYear = idno.substring(6, 10);
		String birthMonth = idno.substring(10, 12);
		String birthDay = idno.substring(12, 14);
		String birthDate = birthYear + "-" + birthMonth + "-" + birthDay;
		return birthDate;
	}

	

	/**
	 * 获取指定日期间的所有日期,包含startDate 和endDate
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static List<String> getDatesBetween(String startDate, String endDate) throws ParseException {
		String date = null;
		List<String> dates = new ArrayList<String>();
		int days = daysBetween(startDate, endDate);
		for (int i = 0; i <= days; i++) {
			date = getFutureTime(startDate, i);
			dates.add(date);
		}
		return dates;
	}

	/**
	 * 计算两个日期之间相隔的天数
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static int daysBetween(String startDate, String endDate) throws ParseException {
		SimpleDateFormat sdf = TL_yyyyMMdd.get();
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(startDate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(endDate));
		long time2 = cal.getTimeInMillis();
		int betweenDays = (int) ((time2 - time1) / TimeUnit.DAYS.toMillis(1));
		return betweenDays;
	}


	/**
	 * 获取指定日期未来x小时候的日期
	 * 
	 * @param startTime
	 * @param hours
	 * @return
	 * @throws ParseException
	 */
	public static String getFutureHoursTime(String startTime, int hours) throws ParseException {
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		Date date = dateFormat.parse(startTime);
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR, hours);
		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 将ms转换成长日期格式
	 * 
	 * @param millis
	 * @return
	 */
	public static String formatDateTime(long millis) {
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		return dateFormat.format(new Date(millis));
	}

	/**
	 * 将ms转换成短日期格式
	 * 
	 * @param millis
	 * @return
	 */
	public static String formatDate(long millis) {
		DateFormat dateFormat = TL_yyyyMMdd.get();
		return dateFormat.format(new Date(millis));
	}

	/**
	 * YYYY-MM-DD HH:mm:ss 转换成Date
	 * 
	 * @param dateTime
	 * @return
	 * @throws Exception
	 */
	public static Date getDate(String dateTime) throws Exception {
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		return dateFormat.parse(dateTime);
	}

	public static int getCurrentSeasonOfYear() {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		int month = calendar.get(Calendar.MONTH) + 1;
		if (month <= 3) {
			return 1;
		} else if (month <= 6) {
			return 2;
		} else if (month <= 9) {
			return 3;
		} else {
			return 4;
		}
	}

	public static int getCurrentYear() {
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		return calendar.get(Calendar.YEAR);
	}

	public static String getStringDate() {
		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);
		int month = now.get(Calendar.MONTH) + 1;
		int day = now.get(Calendar.DAY_OF_MONTH);
		String montht = null;
		if (month < 10) {
			montht = "0" + month;
		} else {
			montht = month + "";
		}
		String dayt = null;
		if (day < 10) {
			dayt = "0" + day;
		} else {
			dayt = day + "";
		}
		String currentDate = year + "年" + montht + "月" + dayt + "日";
		return currentDate;
	}

	public static String getMdDate(int i) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, i);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		return month + "月" + day + "日";
	}

	/**
	 * datetime大于当前时间则返回true，反之false
	 * 
	 * @param datetime
	 * @return
	 */
	public static boolean compareDate(Date datetime) {

		long longtime = datetime.getTime();
		long currentTimeMilles = System.currentTimeMillis();

		return longtime > currentTimeMilles;
	}

	/**
	 * 在指定的时间上 加 x 分钟
	 * 
	 * @param time
	 * @param minutes
	 * @return
	 */
	public static String getTimeAfterMinutes(String time, int minutes) {
		if (StringUtils.isEmpty(time)) {
			return "";
		}
		try {
			DateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
			Date date = dateFormat.parse(time);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.MINUTE, minutes);
			return dateFormat.format(calendar.getTime());
		} catch (ParseException e) {
		}
		return time;
	}

	public static int compareHHMMSS(String hhmmss1, String hhmmss2) throws Exception {
		if (StringUtils.isEmpty(hhmmss1) && StringUtils.isEmpty(hhmmss2)) {
			return 0;
		}
		if (StringUtils.isEmpty(hhmmss1)) {
			return -1;
		}
		if (StringUtils.isEmpty(hhmmss2)) {
			return 1;
		}
		DateFormat df = new SimpleDateFormat("HH:mm:ss");
		Date dt1 = df.parse(hhmmss1);
		Date dt2 = df.parse(hhmmss2);
		if (dt1.getTime() > dt2.getTime()) {
			return 1;
		} else if (dt1.getTime() < dt2.getTime()) {
			return -1;
		} else {
			return 0;
		}
	}

	public static String getHHMMSS(long millis) {
		DateFormat df = new SimpleDateFormat("HH:mm:ss");
		return df.format(new Date(millis));
	}

	public static String getHHMMSS(String hhmm) {
		String defaultStr = "00:00:00";
		if (StringUtils.isEmpty(hhmm)) {
			return defaultStr;
		}
		String[] timePair = hhmm.split(":");
		if (timePair.length == 2) {
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(timePair[0]));
			calendar.set(Calendar.MINUTE, Integer.valueOf(timePair[1]));
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			return getHHMMSS(calendar.getTime().getTime());
		}
		return defaultStr;
	}

	/**
	 * 计算某时刻 n分钟后的具体时间
	 * 
	 * @param date
	 * @param minutes
	 * @return
	 */
	public static String getDateTimeAfterMinutes(Date date, int minutes) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, minutes);
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		return dateFormat.format(calendar.getTime());
	}
	
	/**
	 * 计算某时刻 n分钟后的具体时间
	 * 
	 * @param minutes
	 * @return
	 */
	public static String getDateTimeAfterMinutes(String dateTime, int minutes) {
		if (StringUtils.isEmpty(dateTime)) {
			return "";
		}
		try {
			DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
			Date date = dateFormat.parse(dateTime);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.MINUTE, minutes);
			return dateFormat.format(calendar.getTime());
		} catch (ParseException e) {
		}
		return dateTime;
	}

	/**
	 * 计算某时刻 n秒后的具体时间
	 * 
	 * @return
	 */
	public static String getDateTimeAfterSeconds(String dateTime, int seconds) {
		if (StringUtils.isEmpty(dateTime)) {
			return "";
		}
		try {
			DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
			Date date = dateFormat.parse(dateTime);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.SECOND, seconds);
			return dateFormat.format(calendar.getTime());
		} catch (ParseException e) {
		}
		return dateTime;
	}

	public static String formatDate2ShortDateString(String dateTime) throws ParseException {
		if (StringUtils.isEmpty(dateTime)) {
			return null;
		}
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		Date date = dateFormat.parse(dateTime);
		return date2String(date);
	}

	/**
	 * 获取两个日期间的分钟间隔
	 * 
	 * @param dateTime1
	 * @param dateTime2
	 * @return
	 */
	public static int getMinutesBetween(String dateTime1, String dateTime2) {
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		try {
			Date date1 = dateFormat.parse(dateTime1);
			Date date2 = dateFormat.parse(dateTime2);
			long millis = date2.getTime() - date1.getTime();
			return (int) TimeUnit.MILLISECONDS.toMinutes(millis);
		} catch (Exception e) {
			return 0;
		}
	}

	public static int getDaysBetween(String dateTime1, String dateTime2) {
		DateFormat dateFormat = TL_yyyyMMdd.get();
		try {
			Date date1 = dateFormat.parse(dateTime1);
			Date date2 = dateFormat.parse(dateTime2);
			long millis = date2.getTime() - date1.getTime();
			return (int) millis/1000/60/60/24;
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 获取今天开始时间 yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String getTodayStartDateTime() {
		Date date = new Date();
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String dateString = formatter.format(date);
		dateString += " 00:00:00";
		return dateString;
	}

	/**
	 * 获取今天结束时间 yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String getTodayEndDateTime() {
		Date date = new Date();
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String dateString = formatter.format(date);
		dateString += " 23:59:59";
		return dateString;
	}

	public static long getTodayEndMs() throws Exception {
		String dateTime = getTodayEndDateTime();
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		Date date = dateFormat.parse(dateTime);
		return date.getTime();
	}

	/**
	 * 获取今天结束时间 Date
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Date getTodayEnd() throws Exception {
		String dateTime = getTodayEndDateTime();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
		Date date = dateFormat.parse(dateTime);
		return date;
	}
	
	/**
	 * 根据短日期和时间获取ms
	 * 
	 * @param date
	 * @param time
	 * @return
	 * @throws Exception
	 */
	public static long getDateTimeMs(String date, String time) throws Exception {
		String dateTime = date + " " + time;
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		Date uDate = dateFormat.parse(dateTime);
		return uDate.getTime();
	}

	/**
	 * 慎用，只用于根据短日期和时间获取ms，解决getDateTimeMs方法传入24:00:00报错的问题（StopDutyOrderValidateFilter）
	 * @param date
	 * @param time
	 * @return
	 * @throws Exception
	 */
	public static long get24DateTimeMs(String date, String time) throws Exception {
		String dateTime = date + " " + time;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss");
		Date uDate = sdf.parse(dateTime);
		return uDate.getTime();
	}

	/**
	 * 根据dateTime 获取ms
	 * 
	 * @param dateTime
	 * @return
	 * @throws Exception
	 */
	public static long getDateTimeMs(String dateTime) throws Exception {
		DateFormat dateFormat = TL_yyyyMMddHHmmss.get();
		Date uDate = dateFormat.parse(dateTime);
		return uDate.getTime();
	}

	public static int getMonth(String yymmdd) throws Exception {
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		Date date = formatter.parse(yymmdd);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1;
	}

	public static int getDay(String yymmdd) throws Exception {
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		Date date = formatter.parse(yymmdd);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public static String getMonthStr(String yymmdd) throws Exception {
		int month = getMonth(yymmdd);
		if (month < 10) {
			return "0" + month;
		}
		return String.valueOf(month);
	}

	public static int getYear(String yymmdd) throws Exception {
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		Date date = formatter.parse(yymmdd);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 获取当前季度的开始月份
	 * 
	 * @return
	 */
	public static int getCurrentQuarterFirstMonth() {
		int month = getCurrentMonth();
		int startMonth = 1;
		if (month <= 3) {
			startMonth = 1;
		} else if (month > 3 && month <= 6) {
			startMonth = 4;
		} else if (month > 6 && month <= 9) {
			startMonth = 7;
		} else {
			startMonth = 10;
		}
		return startMonth;
	}

	/**
	 * 获取季度的第一天 yyyy-mm-dd
	 * 
	 * @return
	 */
	public static String getCurrentQuarterFirstDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.MONTH, getCurrentQuarterFirstMonth() - 1);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		DateFormat df = TL_yyyyMMdd.get();
		String result = df.format(calendar.getTime());
		return result;
	}

	/**
	 * 获取当前季度的结束月份
	 * 
	 * @return
	 */
	public static int getCurrentQuarterLastMonth() {
		int month = getCurrentMonth();
		int endMonth = 1;
		if (month <= 3) {
			endMonth = 3;
		} else if (month > 3 && month <= 6) {
			endMonth = 6;
		} else if (month > 6 && month <= 9) {
			endMonth = 9;
		} else {
			endMonth = 12;
		}
		return endMonth;
	}

	/**
	 * 计算n季度之后的第一天
	 * 
	 * @param quarter
	 * @return
	 */
	public static String getFirstDayAfterQuarter(int quarter) {
		Calendar calendar = Calendar.getInstance();
		int month = getCurrentQuarterFirstMonth();
		calendar.set(Calendar.MONTH, month);
		int delta = quarter * 3;
		calendar.add(Calendar.MONTH, delta);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		DateFormat df = TL_yyyyMMdd.get();
		String result = df.format(calendar.getTime());
		return result;
	}

	/**
	 * 计算n季度之后的最后一天
	 * 
	 * @param quarter
	 * @return
	 */
	public static String getLastDayAfterQuarter(int quarter) {
		Calendar calendar = Calendar.getInstance();
		int month = getCurrentQuarterFirstMonth();
		calendar.set(Calendar.MONTH, month);
		int delta = quarter * 3;
		calendar.add(Calendar.MONTH, delta);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		DateFormat df = TL_yyyyMMdd.get();
		String result = df.format(calendar.getTime());
		return result;
	}

	/**
	 * 获取季度的最后一天 yyyy-mm-dd
	 * 
	 * @return
	 */
	public static String getCurrentQuarterLastDay() {
		Calendar calendar = Calendar.getInstance();
		int month = getCurrentQuarterLastMonth();
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		DateFormat df = TL_yyyyMMdd.get();
		String result = df.format(calendar.getTime());
		return result;
	}

	/**
	 * 获取n月后的第一天
	 * 
	 * @param month
	 * @return
	 */
	public static String getFirstDayAfterMonth(int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String dateString = formatter.format(calendar.getTime());
		return dateString;
	}

	/**
	 * 获取指定日期n月后的第一天
	 *
	 * @param month
	 * @return
	 */
	public static String getFirstDayAfterMonthForDate(Date date, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String dateString = formatter.format(calendar.getTime());
		return dateString;
	}

	/**
	 * 获取指定日期n月后的最后一天
	 *
	 * @param month
	 * @return
	 */
	public static String getLastDayAfterMonthForDate(Date date, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String dateString = formatter.format(calendar.getTime());
		return dateString;
	}

	/**
	 * 获取n月后的最后第一天
	 * 
	 * @param month
	 * @return
	 */
	public static String getLastDayAfterMonth(int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String dateString = formatter.format(calendar.getTime());
		return dateString;
	}

	/**
	 * 获取本年度第一天
	 * 
	 * @return
	 */
	public static String getCurrentYearFirstDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMinimum(Calendar.DAY_OF_YEAR));
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String dateString = formatter.format(calendar.getTime());
		return dateString;
	}

	/**
	 * 获取本年度最后一天
	 * 
	 * @return
	 */
	public static String getCurrentYearLastDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String dateString = formatter.format(calendar.getTime());
		return dateString;
	}

	/**
	 * 获取n月后的某一天
	 * 
	 * @param month
	 * @return
	 */
	public static String getDayAfterMonth(int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, month);
		SimpleDateFormat formatter = TL_yyyyMMdd.get();
		String dateString = formatter.format(calendar.getTime());
		return dateString;
	}

	/**
	 * 获取指定日期n月后的某一天
	 * 
	 * @param startDate
	 * @param month
	 * @return
	 */
	public static String getDayAfterMonth(String startDate, int month) {
		try {
			SimpleDateFormat formatter = TL_yyyyMMdd.get();
			Date date = formatter.parse(startDate);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.MONTH, month);
			String dateString = formatter.format(calendar.getTime());
			return dateString;
		} catch (ParseException e) {
		}
		return startDate;
	}

	/**
	 * 将/类型的日期转为-类型的日期
	 * 
	 * @param slashDate
	 * @return
	 * @throws ParseException
	 */
	public static String convertSlashToMinus(String slashDate) throws ParseException {
		if (StringUtils.isEmpty(slashDate)) {
			return null;
		}
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
		Date date = dateFormat.parse(slashDate);
		return date2String(date);
	}

	/**
	 * 2016-06-01 变 2016-6-1
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String toYMD(String dateStr) {
		try {
			SimpleDateFormat formatter = TL_yyyyMMdd.get();
			Date date = formatter.parse(dateStr);
			SimpleDateFormat formatterYMD = new SimpleDateFormat("yyyy-M-d");
			return formatterYMD.format(date);
		} catch (ParseException e) {
		}
		return dateStr;
	}

	/**
	 * 2016-06-01 变 2016-6-1
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String toYYYYMMDD(String dateStr) {
		try {
			SimpleDateFormat formatter = TL_yyyyMd.get();
			Date date = formatter.parse(dateStr);
			SimpleDateFormat formatterYMD = new SimpleDateFormat("yyyy-MM-dd");
			return formatterYMD.format(date);
		} catch (ParseException e) {
		}
		return dateStr;
	}

	/**
	 * 返回指定时间的秒数
	 * 
	 * @param date
	 * @return
	 */
	public static int getSecondsOfDate(Date date) {
		if (date == null) {
			return 0;
		}
		Calendar calendar = new GregorianCalendar();

		calendar.setTime(date);

		return calendar.get(Calendar.SECOND);
	}

	/**
	 * 返回指定时间的秒数
	 * 
	 * @return
	 * @throws ParseException 
	 */
	public static int getSecondsOfDate(String dateStr) throws ParseException {
		if (StringUtils.isEmpty(dateStr)) {
			return 0;
		}

		Date date = stringForDate(dateStr);

		return getSecondsOfDate(date);
	}

	/**
	 * 获取n天前的日期
	 * 
	 * @param n
	 *            天数
	 * @return
	 */
	public static String beforeNdays(int n) {
		SimpleDateFormat yyyyMMdd = TL_yyyyMMdd.get();
		Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
		calendar.add(Calendar.DATE, -n);
		String date = yyyyMMdd.format(calendar.getTime());
		return date + " 00:00:00";
	}

	/**
	 * 时间戳转换为日期时间字符串
	 * 
	 * @param timeStamp
	 * @return
	 */
	public static String timeStampToDateTime(String timeStamp) {
		SimpleDateFormat sdf = TL_yyyyMMddHHmmss.get();
		String dateTime = sdf.format(new Date(Long.parseLong(timeStamp) * 1000));
		return dateTime;
	}
	/**
	 * 将字符串类型(例：2017-08-09 16:00:00)的年月日时分秒转换为日期类型java.util.Date
	 * 
	 * @param timeStamp
	 * @return
	 * @throws ParseException 
	 */
	public static Date stringFormatToDateTime(String timeStamp) throws ParseException {
		DateFormat df = TL_yyyyMMddHHmmss.get();
		Date date = df.parse(timeStamp);
		return date;
	}
	
	public static long getTodayMs(String time) throws Exception {
		String today = getFutureTime(0);
		SimpleDateFormat yyyyMMdd = TL_yyyyMMddHHmmss.get();
		if (StringUtils.isNotEmpty(time)) {
			today = today + " " + time;
		} else {
			today = today + " 00:00:00";
		}
		Date date = yyyyMMdd.parse(today);
		return date.getTime();
	}

	/**
	 * 根据月日得到年月日
	 * 
	 * @param Mmdd
	 * @return
	 */
	public static String getYymmdd(String Mmdd) throws Exception {
		String str = "";
		if (StringUtils.isEmpty(Mmdd)) {
			return str;
		}
		int year = DateUtils.getYear(DateUtils.shortDate2String());
		String today = DateUtils.getFutureTimeMmdd(0);
		if (today.compareTo(Mmdd) <= 0) {
			str = year + "-" + Mmdd;
		} else {
			year = year + 1;
			str = year + "-" + Mmdd;
		}
		return str;
	}

	/**
	 * 获取指定日期前一天的16:00:00(返回日期类型)
	 * 
	 * @author jiliangshan
	 * @date 2017-1-11 下午4:15:51
	 */
	public static Date getSpecifiedDateBefore(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = TL_yyyyMMddHHmmss.get();
		Date d = null;
		try {
			d = TL_yyyyMMdd.get().parse(specifiedDay);
			c.setTime(d);
			c.set(Calendar.DATE, c.get(Calendar.DATE) - 1);
			String dayBefore = sdf.format(c.getTime());
			d = sdf.parse(dayBefore);
			String specifiedDayBefore = sdf.format(new Date(d.getTime() + 16 * 60 * 60 * 1000));
			d = sdf.parse(specifiedDayBefore);
		} catch (ParseException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return d;
	}

	/**
	 * 获取指定日期前一天的16:00:00(返回字符串类型)
	 * 
	 * @author jiliangshan
	 * @date 2017-1-11 下午4:15:51
	 */
	public static String getSpecifiedDateStringBefore(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = TL_yyyyMMddHHmmss.get();
		String dateString = "";
		try {
			Date d = TL_yyyyMMdd.get().parse(specifiedDay);
			c.setTime(d);
			c.set(Calendar.DATE, c.get(Calendar.DATE) - 1);
			String dayBefore = sdf.format(c.getTime());
			d = sdf.parse(dayBefore);
			String specifiedDayBefore = sdf.format(new Date(d.getTime() + 16 * 60 * 60 * 1000));
			d = sdf.parse(specifiedDayBefore);
			dateString = dateChangeString(getSpecifiedDateBefore(specifiedDay));
		} catch (ParseException e) {
			LOGGER.error(e.getMessage(), e);
		}
		return dateString;
	}

	/**
	 * 获取当前日期三个月前的日期(返回日期类型)
	 * 
	 * @author jiliangshan
	 * @throws ParseException 
	 * @date 2017-1-16 下午5:26:11
	 */
	public static Date getThreeMonthAgo() throws ParseException {
		
		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(dNow);// 把当前时间赋给日历
		calendar.add(Calendar.MONTH, -3); // 设置为前3月
		dBefore = calendar.getTime(); // 得到前3月的时间

		SimpleDateFormat sdf = TL_yyyyMMdd.get(); // 设置时间格式
		String ThreeMonthAgoDate = sdf.format(dBefore); // 格式化前3月的时间
		Date date = null;
		
		date = sdf.parse(ThreeMonthAgoDate);
		
		return date;
	}

	/**
	 * 获取上周五时间
	 */
	public static Date lastFriday() {

		Calendar calendar = Calendar.getInstance();
		while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
			calendar.add(Calendar.DAY_OF_WEEK, -1);
		}
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		int offset = 7 - dayOfWeek;
		calendar.add(Calendar.DATE, offset - 9);
		// 这是从上周日开始数的到本周五为6
		return getFirstDayOfWeek(calendar.getTime(), 6);
	}

	/**
	 * 获取上周一时间
	 */
	public static Date lastMonday() {
		Calendar calendar = Calendar.getInstance();
		while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
			calendar.add(Calendar.DAY_OF_WEEK, -1);
		}
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		int offset = 1 - dayOfWeek;
		calendar.add(Calendar.DATE, offset - 7);
		return getFirstDayOfWeek(calendar.getTime(), 2);
	}

	/**
	 * 得到某一天的该星期的第一日 00:00:00
	 * 
	 * @param date
	 * @param firstDayOfWeek
	 *            一个星期的第一天为星期几
	 * 
	 * @return
	 */
	public static Date getFirstDayOfWeek(Date date, int firstDayOfWeek) {
		Calendar cal = Calendar.getInstance();
		if (date != null)
			cal.setTime(date);
		cal.setFirstDayOfWeek(firstDayOfWeek);// 设置一星期的第一天是哪一天
		cal.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);// 指示一个星期中的某天
		cal.set(Calendar.HOUR_OF_DAY, 0);// 指示一天中的小时。HOUR_OF_DAY 用于 24
											// 小时制时钟。例如，在 10:04:15.250 PM
											// 这一时刻，HOUR_OF_DAY 为 22。
		cal.set(Calendar.MINUTE, 0);// 指示一小时中的分钟。例如，在 10:04:15.250 PM
									// 这一时刻，MINUTE 为 4。
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	/**
	 * 得到当前时间的前一天日期  yyyy-MM-dd
	 * @return
	 */
	public static String getNowDateBefore(){
		  Calendar   cal   =   Calendar.getInstance();
		  cal.add(Calendar.DATE,   -1);
		  String yesterday =TL_yyyyMMdd.get().format(cal.getTime());
		  return yesterday;
	}
	
	public static String afterMinutes(String datetime, int n) throws ParseException {
	    
	    SimpleDateFormat sdf = TL_yyyyMMddHHmmss.get();
	    Date date = sdf.parse(datetime);
	    Calendar cal = Calendar.getInstance();
	    cal.setTimeInMillis(date.getTime());
	    
	    cal.add(Calendar.MINUTE, n);
	    Date result = cal.getTime();
	    return sdf.format(result);
	}
	
	/**
	 * 指定某天加N天
	 * @param datetime
	 * @param n
	 * @return
	 * @throws ParseException
	 */
	public static String afterDays(String datetime, int n) throws ParseException {
	    
	    SimpleDateFormat sdf = TL_yyyyMMddHHmmss.get();
	    Date date = sdf.parse(datetime);
	    Calendar cal = Calendar.getInstance();
	    cal.setTimeInMillis(date.getTime());
	    
	    cal.add(Calendar.DATE, n);
	    Date result = cal.getTime();
	    return sdf.format(result);
	}
	 /**
     * 判断指定日期是否是今天
     * @param date
     * @return    是返回true，不是返回false
	 * @throws ParseException 
     */
	public static boolean isToday(String dateString) throws ParseException {
		if(StringUtils.isEmpty(dateString)) {
			return false;
		} 
    	Date date = stringForDate(dateString);
        //当前时间
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        //获取今天的日期
        String nowDay = sf.format(now);
        //对比的时间
        String day = sf.format(date);
        return day.equals(nowDay);
    }
	/**
	 * 判断指定日期是否是周六、周日
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public static boolean isWeekend(String dateString) throws ParseException{  
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");         
        Date bdate = format1.parse(dateString);  
        Calendar cal = Calendar.getInstance();  
        cal.setTime(bdate);  
        if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){  
            return true;  
        }  
        else return false;  
    }

    /**
     * 判断传入的时间是上午还是下午
     * 1:上午 2:下午
     * @param dateTime
     * @return
     */
    public static int dateTimeToTimeInteval(Date dateTime){
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateTime);
        int timeInterval = cal.get(Calendar.AM_PM);
        switch (timeInterval){
            case 0 :
                return 1;
            case 1 :
                return 2;
        }
        return -1;
    }


	/**
	 * 将字符串类型的日期转换成一定格式的Date
	 * @param dateString String类型的时间
	 * @param pattern 表达式
	 * @return
	 * @throws ParseException
	 */
	public final static Date string2DateByPattern(String dateString, String pattern) throws ParseException {
	
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date date = sdf.parse(dateString);
		return date;
		
	}

	/**
	 * 将日期转换成一定格式的字符串类型
	 * @param date String类型的时间
	 * @param pattern 表达式
	 * @return
	 */
	public final static String date2StringDateByPattern(Date date, String pattern) {

		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String dateString = sdf.format(date);
		return dateString;

	}
	
	/**
	 * 返回end-start相差的秒数
	 * @param start
	 * @param end
	 * @return
	 */
	public static int subSecond(Date start, Date end) {
		 long s = start.getTime();
	     long e = end.getTime();
	     int sub = (int) ((e - s) / 1000);
	     return sub;
	}
	
	/**
	 * 得到当前时间的下一年
	 * @return
	 */
	public static String getNextYearCurrentTime(){
		Calendar calendar = Calendar.getInstance();
        Date date = new Date(System.currentTimeMillis());
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, 1);
        date = calendar.getTime();
        return datetime2string(date);
	}
	
	/**
	 * 得到传入时间的下一年
	 * @return
	 * @throws ParseException 
	 */
	public static String getNextYearTime(String dateStr) throws ParseException{
		Calendar calendar = Calendar.getInstance();
        Date date = string2datetime(dateStr);
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, 1);
        date = calendar.getTime();
        return datetime2string(date);
	}
	
	/**
	 * java.time.LocalDate --> java.util.Date
	 * @param localDate
	 * @return
	 */
	public static Date localDateToDate(LocalDate localDate) {
	    ZoneId zone = ZoneId.systemDefault();
	    Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
		return Date.from(instant);
	}
	
	/**
	 * 以当天为基准，获得x天后得起始时间
	 * @param x
	 * @return yyyy-MM-dd 00:00:00
	 */
	public static String getStartTime(int x) {  
		
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(new Date());  
        todayStart.add(Calendar.DAY_OF_MONTH, x);  
        todayStart.set(Calendar.HOUR_OF_DAY, 0);  
        todayStart.set(Calendar.MINUTE, 0);  
        todayStart.set(Calendar.SECOND, 0);  
        todayStart.set(Calendar.MILLISECOND, 0);  
        Date date = todayStart.getTime();
        return datetime2string(date);  
    }   
  
	/**
	 * 以当天为基准，获得x天后得结束时间
	 * @param x
	 * @return yyyy-MM-dd 23:59:59
	 */
    public static String getEndTime(int x) {  
        Calendar todayEnd = Calendar.getInstance();  
        todayEnd.setTime(new Date());  
        todayEnd.add(Calendar.DAY_OF_MONTH, x);  
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);  
        todayEnd.set(Calendar.MINUTE, 59);  
        todayEnd.set(Calendar.SECOND, 59);  
        todayEnd.set(Calendar.MILLISECOND, 999);  
        Date date = todayEnd.getTime();
        return datetime2string(date);
    }  
	
    /**
     * 获得时间的时分如2018-3-21 17:27:00 -->17:27
     * @param time
     * @return
     * @throws ParseException 
     */
    public static String getTimeMs(String time) throws ParseException {
    	SimpleDateFormat ymdHmsSdf = TL_yyyyMMddHHmmss.get();
    	SimpleDateFormat sdf = TL_HHmm.get();
		return sdf.format(ymdHmsSdf.parse(time));
    	
    }
    
    /**
     * 比较两个HH:MM的大小
     * @param hhmm1
     * @param hhmm2
     * @return
     * @throws Exception
     */
    public static int compareHHMM(String hhmm1, String hhmm2) throws Exception {
		if (StringUtils.isEmpty(hhmm1) && StringUtils.isEmpty(hhmm2)) {
			return 0;
		}
		if (StringUtils.isEmpty(hhmm1)) {
			return -1;
		}
		if (StringUtils.isEmpty(hhmm2)) {
			return 1;
		}
		SimpleDateFormat sdf = TL_HHmm.get();
		Date dt1 = sdf.parse(hhmm1);
		Date dt2 = sdf.parse(hhmm2);
		if (dt1.getTime() > dt2.getTime()) {
			return 1;
		} else if (dt1.getTime() < dt2.getTime()) {
			return -1;
		} else {
			return 0;
		}
	}
    
    /**
	 * 传入的时间是否在某个时间段之间（HH:mm:ss）(包括开始时间，不包括结束时间)
	 * @param inputTime
	 * @param startTime
	 * @param endTime
	 * @return
	 * @throws ParseException
	 */
	public static boolean inputTimeIsBetweenTime(String inputTime, String startTime, String endTime)throws ParseException{
		int result1 = compareTime(inputTime,startTime);
		if(result1 == -1){
			return false;
		}
		int result2 = compareTime(inputTime,endTime);
		if(result2 == 1 || result2 ==0){
			return false;
		}
		return true;
	}
	
	public static String getLongDate(Date date) {
		SimpleDateFormat sdf = TL_yyyyMMddHHmmssS.get();
		return sdf.format(date);
	}
	
	/**
	 * String --> java.time.LocalDate
	 * @param dateString
	 * @return
	 * @throws ParseException 
	 */
	public static LocalDate getLocalDate(String dateString){
		if(StringUtils.isEmpty(dateString)) {
			return null;
		}
		dateString = toYYYYMMDD(dateString);
		LocalDate localDate = LocalDate.parse(dateString);
	    return localDate;
	}

    /**
     * 将201805220835格式化，并获取HH：mm
     * @param time
     * @return
     * @throws ParseException
     */
    public static String getTimeHm(String time) throws ParseException {
        SimpleDateFormat ymdHmsSdf = new SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH);
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
        return sdf.format(ymdHmsSdf.parse(time));

    }
	
	 /**
	  * 将指定的时间往后推minutes分钟，如08:35往后推30分钟为09:05
	  * @param hhmm
	  * @param minutes
	  * @return
	  * @throws ParseException
	  */
	 public static String getFutureTimeMs(String hhmm,int minutes) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
		Date date = sdf.parse(hhmm);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, minutes);
		return sdf.format(cal.getTime());
	}

	/**
	  * 获得某个月最大天数
	  *
	  * @param date yyyy-MM-dd
	  * @return 某个月最大天数
	  */
	public static int getMaxDayByYearMonth(String date) throws Exception {
		Calendar _calendar = Calendar.getInstance();
		_calendar.setTime(string2Date(date));
		Calendar calendar = Calendar.getInstance();

		calendar.set(Calendar.YEAR, _calendar.get(Calendar.YEAR));
		calendar.set(Calendar.MARCH, _calendar.get(Calendar.MARCH)+1);
		calendar.set(Calendar.DAY_OF_MONTH, 0);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	  * 获得当前日期指定时间
	  *
	  * @param time HH:mm:ss
	  * @return yyyy-MM-dd HH:mm:ss
	  */
	public static String getCurrentDateAppointTime(String time) throws Exception {
		Date current =  new Date();
		DateFormat dateFormat = TL_yyyyMMdd.get();
		dateFormat.setLenient(false);
		String dateString = dateFormat.format(current);
		return dateString + " " + time;
	}

	/**
	  * 获得指定时间毫秒
	  *
	  * @param dateTime
	  * @return long
	  */
	public static long getAppointTimeInMillis(Date dateTime) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.setTime(dateTime);
		long millis = calendar.getTimeInMillis();
		return millis;
	}

	/****
	 * @author: zhangdengfeng
	 * @date: 2018年9月4日 上午10:24:49 
	 * @Description: 把秒转换成天、时、分、秒
	 * @version V1.0 
	 * @param mss
	 * @return
	 */
	public static String secondformatDateTime(long mss) {
		String DateTimes = null;
		long days = mss / (60 * 60 * 24);
		long hours = (mss % (60 * 60 * 24)) / (60 * 60);
		long minutes = (mss % (60 * 60)) / 60;
		long seconds = mss % 60;
		if (days > 0) {
			DateTimes = days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒";
		} else if (hours > 0) {
			DateTimes = hours + "小时" + minutes + "分钟" + seconds + "秒";
		} else if (minutes > 0) {
			DateTimes = minutes + "分钟" + seconds + "秒";
		} else {
			DateTimes = seconds + "秒";
		}

		return DateTimes;
	}
	
}
